package bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class ConnectionPool_list {
	Connection conn=null;
	public ConnectionPool_list() {
		try {
			Context context = new InitialContext();
			 DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/OracleDB");
			 conn = ds.getConnection();
			 System.out.println("suscess pool");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public ArrayList<MemberVo> select(){
			
			ArrayList<MemberVo> al = new ArrayList<MemberVo>();
			String sql= "select * from member";
			
				try {
					PreparedStatement pst = conn.prepareStatement(sql);
					ResultSet rs = pst.executeQuery();
					while(rs.next()){
						MemberVo vo = new MemberVo();
						vo.setMid(rs.getString("mid"));
						System.out.println(vo.getMid());
						vo.setIrum(rs.getString("irum"));
						al.add(vo);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			return al;
		}
	
	public ArrayList<MemberVo> select(String findStr){
		ArrayList<MemberVo> al = new ArrayList<MemberVo>();
		String sql= "select * from member where mid like ? or irum like ?";
			try {
				PreparedStatement pst = conn.prepareStatement(sql);
				pst.setString(1, "%" +findStr + "%");
				pst.setString(2, "%"+findStr+"%");
				
				ResultSet rs = pst.executeQuery();
				while(rs.next()){
					MemberVo vo = new MemberVo();
					vo.setMid(rs.getString("mid"));
					vo.setIrum(rs.getString("irum"));
					al.add(vo);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		return al;
	}
	
	public static void main(String [] args){
		new ConnectionPool_list();
	}
}
